﻿package Stage 
{
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	
	import Common.*;
	import Events.*;

	/**
	 * ...
	 * @author fox
	 */
	public class GameStageManager 
	{
		private var activeStage:GameState = null;
		private var loginStage:LoginStage = new LoginStage();
		private var logoStage:LogoStage = new LogoStage();
		private var mainStage:MainStage = new MainStage();
		
		public static var instance:GameStageManager = null;
		
		public static function Instance():GameStageManager
		{
			if ( instance == null )
			{
				instance = new GameStageManager();
			}
			
			return instance;
		}
		
		
		public function GameStageManager() 
		{
			
		}
		
		public function init():void
		{
			setActiveStage( GameState.LOGIN );
			
			ModuleEventManager.instance.addEventListener( ModuleEvent.LOGINRELEASE , onLoginRelease );
			ModuleEventManager.instance.addEventListener( ModuleEvent.MAINRELEASE , onMainRelease );
		}
		
		private function onLoginRelease( event:ModuleEvent ):void
		{
			setActiveStage( GameState.MAIN );
			//CModuleEventManager.instance.removeEventListener( CModuleEvent.LOGINRELEASE , onLoginRelease );
		}
		
		private function onMainRelease( event:ModuleEvent ):void
		{
			setActiveStage( GameState.LOGIN );
			//CModuleEventManager.instance.removeEventListener( CModuleEvent.LOGINRELEASE , onLoginRelease );
		}
		
		public function release():void
		{
			
		}
		
		public function setActiveStage( type:String ):void
		{
			if ( activeStage )
			{
				activeStage.release();
			}
			
			switch ( type )
			{
				case GameState.LOGO:
				{
					activeStage = logoStage;
				}
				break;
				case GameState.LOGIN:
				{
					activeStage = loginStage;
				}
				break;
				case GameState.MAIN:
				{
					activeStage = mainStage;
				}
				break;
			}
				
			activeStage.init();
		}
		
		public function getActiveStage():GameState
		{
			return activeStage;
		}
		
		public function isActive( type:String ):Boolean
		{
			if (!activeStage)
				return false;
			
			return activeStage.Type == type;
		}
	}
	
}